package cn.itcast.bos.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.IFixedareaService;
import cn.itcast.crm.domain.Customer;

/** 
 * 定区的控制层
 *
 * @author Shao-Fei Fan 
 * @version 1.0,2017年9月6日 下午10:00:15 
 */

@Controller
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/")
public class Fixed_areaAction extends BaseAction<FixedArea> {
	
	// 得到事物层
	@Autowired
	private IFixedareaService fixedareaService;
	
	@Action(value="save_fixed_area",results={@Result(name="success",location="./pages/base/fixed_area.html",type="redirect")})
	public String saveFixedarea(){
		
		fixedareaService.save(model);
		return SUCCESS;
	}
	
	/*id	
	company	
	subareaName*/
	/**
	 * 分页条件查询
	 */
	@Action(value="fixed_area_search",results={@Result( name="success", type="json")})
	public String search(){
		// 封装分页数据
		Pageable pageable = new PageRequest(page-1, rows);
		
		// 封装条件
		Specification<FixedArea> specification = new Specification<FixedArea>() {
			
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if(StringUtils.isNoneBlank(model.getId())){
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(p1);
				}
				if(StringUtils.isNoneBlank(model.getCompany())){
					Predicate p2 = cb.like(root.get("company").as(String.class),"%"+ model.getCompany()+"%");
					list.add(p2);
				}
				Predicate predicate = cb.and(list.toArray(new Predicate[0]));
				return predicate;
			}
		};
		
		// 调用业务层
		Page<FixedArea> search = fixedareaService.search(specification,pageable);
		pushPageDataToVolueStake(search);
		return SUCCESS;
	}
	
	/**
	 * 查询未关联客户列表
	 */
	@Action(value="fixedArea_findNoAssociationCustomers",results={@Result(name="success",type="json")})
	public String findNoAssociationCustomer(){
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	/**
	 * 查询已关联客户列表
	 */
	@Action(value="fixedArea_findhasAssociationFixedAreaCustomers",results={@Result(name="success",type="json")})
	public String findhasAssociationCustomer(){
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/hasassociationfixedareacustomers/"
		+model.getId()).accept(MediaType.APPLICATION_JSON)
		.type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	/**
	 * 客户与定区的关联操作
	 */
	private String[] customerIds;
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	
	/**
	 * 定区关联客户的操作
	 * @return
	 */
	@Action(value="fixiedarea_associationcustomertofixidarea",results={@Result(name="success",location="./pages/base/fixed_area.html",type="redirect")})
	public String associationCustomerToFixidArea(){
		String customerids = StringUtils.join(customerIds,",");
		
		WebClient
		.create("http://localhost:9002/crm_management/services/customerService/associationcustomerstofixedarea"
		+"?customerIdStr="+customerids+"&fixedAreaId="+model.getId())
		.put(null);
		return SUCCESS;
	}
	
	// 获取takeTimeId和courierId
	private Integer takeTimeId;
	private Integer courierId;
	
	
	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String fixedArea_associationCourierToFixedArea(){
		fixedareaService.associationCourierToFixedArea(model,takeTimeId,courierId);
		
		return SUCCESS;
	}
	
}
